import cv2
import numpy as np


def draw_lines(img, points):
    for point in points:
        point[0] = int(point[0] * 1920)
        point[1] = int(point[1] * 1080)
    points = np.array(points, np.int32)
    print(points)
    img = cv2.polylines(img,
                        [points],
                        1,
                        (255, 0, 255))
    return img


fname = 'down.jpg'
img = cv2.imread(fname)

list_frames = [
    {"x": "0.177083", "y": "0.522222", "w": "0.177083", "h": "0.450000"},
    {"x": "0.213542", "y": "0.603704", "w": "0.062500", "h": "0.085185"},
    {"x": "0.842708", "y": "0.742593", "w": "0.089583", "h": "0.101852"}
]

for dict in list_frames:
    print(img.shape[0])
    width = float(img.shape[1]) * float(dict['w'])
    height = float(img.shape[0]) * float(dict['h'])

    dict_ = {"x1": int(float(img.shape[1]) * float(dict['x']))
        , "x2": int(float(img.shape[1]) * float(dict['x']) + width)
        , "y1": int(float(img.shape[0]) * float(dict['y']))
        , "y2": int(float(img.shape[0]) * float(dict['y']) + height)}
    print(dict_)
    cv2.rectangle(img, (dict_['x1'], dict_['y1']), (dict_['x2'], dict_['y2']), (0, 255, 0), 2)

# cv2.imwrite("123.jpg",img)


points = [[0.466700, 0.647200], [0.406300, 0.991900], [0.598500, 0.989900], [0.552600, 0.637100]]

draw_lines(img, points)
cv2.imshow("123", img)

key = cv2.waitKey(0)
if key == 27:
    cv2.destroyAllWindows()
elif key == ord('s'):  # 它以一个字符（长度为1的字符串）作为参数，返回对应的 ASCII 数值
    cv2.imwrite("123.jpg", img)
    cv2.destroyAllWindows()
